<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Auth_model extends CI_Model {

	/*function register()
	{
		$data = array(
			'meno' => $_POST['meno'],
			'priezvisko' => $_POST['priezvisko'],
			'email' => $_POST['email'],
			'heslo' => sha1($_POST['heslo']),
		);
		
		return $this->db->insert('users', $data);
	}*/
	
	
	function check()
	{
		$admin = $this->db->where('Uzivatelske_meno', $_POST['username'])
						   ->where('Heslo', $_POST['password'])
						   ->get('Administrativni_pracovnici');
		
		$personal = $this->db->where('Uzivatelske_meno', $_POST['username'])
						   ->where('Heslo', $_POST['password'])
						   ->get('Zdravotnicky_personal');
						 
		$pacient = $this->db->where('Uzivatelske_meno', $_POST['username'])
						   ->where('Heslo', $_POST['password'])
						   ->get('Pacient');
						   		   
		return ( $admin->num_rows() || $personal->num_rows() || $pacient->num_rows() );
	}
	
	
	function getUserData($slug)//		Rodne_cislo,Uzivatelske_meno,Heslo,Meno,Priezvisko,Oddelenie,Specializacia
	{
		$admin = $this->db->select('Uzivatelske_meno, Heslo, Meno, Priezvisko')
						   ->where('Uzivatelske_meno', $slug)
						   ->limit(1)
						   ->get('Administrativni_pracovnici');
		
		
		$personal = $this->db->select('Rodne_cislo, Uzivatelske_meno, Heslo, Meno, Priezvisko, Oddelenie, Specializacia')
						   ->where('Uzivatelske_meno', $slug)
						   ->limit(1)
						   ->get('Zdravotnicky_personal');
						   
		$pacient = $this->db->select('Rodne_cislo, Uzivatelske_meno, Meno, Priezvisko, Heslo, Krvna_skupina, Pohlavie, Diagnoza, Dieta')
						   ->where('Uzivatelske_meno', $slug)
						   ->limit(1)
						   ->get('Pacient');
						   
		if ($admin->num_rows()) {
			$ret = $admin->row_array();
			$ret['type'] = 3;
			return $ret;
		}
		else if ($personal->num_rows()) {
			$ret = $personal->row_array();
			
			if ($ret['Specializacia'] == 'Sestra')
				$ret['type'] = 1;
			else
				$ret['type'] = 2;
			
			return $ret;
		}
		else if ($pacient->num_rows()) {
			$ret = $pacient->row_array();
			$query = $this->db->select('Oddelenie, Cislo_izby, Lozko')
						->from('Ubytovany')
						->where('Rodne_cislo', $ret['Rodne_cislo'])
						->get();
									
			$ubytovany = $query->row_array();
			$ret['Oddelenie'] = $ubytovany['Oddelenie'];
			$ret['Cislo_izby'] = $ubytovany['Cislo_izby'];
			$ret['Lozko'] = $ubytovany['Lozko'];
			
			$ret['type'] = 0;
			return $ret;
		}
		else return false;
	}

}